﻿using System;
using System.Security;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using RecruitmentAgency.Models;

namespace RecruitmentAgency
{    
    /// <summary>
    /// The RecruitmentAgency.MvcApplication is responsible for the Routing of the HTTP requests 
    /// </summary>
    public class MvcApplication : HttpApplication
    {
        /// <summary>
        /// The RegisterRoutes performs routing on the HTTP Requests
        /// </summary>
        /// <param name="routes">A collection of routes for ASP.NET</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }


        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError().GetBaseException();
            if (ex is SecurityException)
            {
                if (string.IsNullOrEmpty(User.Identity.AuthenticationType))
                {
                    Response.Redirect("/Account/LogOn");
                }
                else
                {
                    Response.Redirect("/Account/LogOn/E");
                }               
            }
        }


        protected void Application_Start()
        {
            ModelBinders.Binders.DefaultBinder = new RecruitmentAgencyModelBinder();
            RegisterRoutes(RouteTable.Routes);
        }
    }
}